<?php
/*
Plugin Name: BPDEV-MEMBER-THEME
Plugin URI: http://bp-dev.org/projects/#theme
Description: Change member-theme css
Author: Nicola Greco
Version: 0.3
Author URI: http://nicolagreco.com

Copyright (c) 2009-2011, Nicola Greco (mail: notsecurity@gmail.com | website: http://nicolagreco.com).

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

define( 'BPDEV_THEME_NAME' , 'BPDEV-THEME');
define( 'BPDEV_THEME_VERSION' , '0.3');

require_once( 'bp-core.php' );
require_once( 'bpdev-core.php' );
require_once( 'bpdev-theme/bpdev-theme-classes.php' );
require_once( 'bpdev-theme/bpdev-theme-extra.php' );

add_action( 'wp', 'bpdev_theme_setup_globals', 1 );
add_action( '_admin_menu', 'bpdev_theme_setup_globals', 1 );

if ( !get_option( 'bpdev-theme-status' ) ) add_option( 'bpdev-theme-status', 'on' );

if ( get_option('bpdev-theme-status') == "on" ) {

	add_action( 'wp_print_styles' , 'bpdev_theme_load_css' );

	if ( get_option( 'bpdev-theme-buddybar-logo' ) )
		add_filter( 'bp_admin_bar_logo_src', 'bpdev_theme_logo_adminbar' );

}

add_action( 'bpdev_admin_plugins_screen', 'bpdev_theme_admin_menu_screen', 3 );
add_action( 'bpdev_admin_plugins_save', 'bpdev_theme_admin_menu_save', 3 );

function bpdev_theme_setup_globals() {

	global $bp;

	$bp->bpdev->theme->slug = 'theme';
	$bp->bpdev->theme->css = site_url() . '/' . bpdev_component_dir( $bp->bpdev->theme->slug ) . 'css/';
	$bp->bpdev->theme->js = site_url() . '/' . bpdev_component_dir( $bp->bpdev->theme->slug ) . 'js/';
	$bp->bpdev->theme->custom_css = bpdev_component_dir( $bp->bpdev->theme->slug ) . 'css/custom';

}

function bpdev_theme_load_css() {

	global $bp;

	wp_register_style( 'bpdev-theme-css' , $bp->bpdev->theme->css . 'bpdev-theme-css.php' ); 
	wp_enqueue_style( 'bpdev-theme-css' );

	if ( is_dir( "./{$bp->bpdev->theme->custom_css}" ) ) {

		if ( $dh = opendir( "./{$bp->bpdev->theme->custom_css}" ) ) {

			while ( ( $css_file = readdir( $dh ) ) !== false ) {
				if( substr ( $css_file, -4 ) == '.css' ) {
					$css_file_slug = str_replace( '.css', '', $css_file ); 

					wp_register_style( "bpdev-{$css_file_slug}-custom" , $bp->bpdev->widgets->custom_css . "/{$css_file_slug}" );

					wp_enqueue_style( "bpdev-{$css_file_slug}-custom" );

				}
			}

		}

	}

}

function bpdev_theme_admin_menu_save() {

	do_action( 'bpdev_admin_save_component', 'bpdev-theme-status', 1 );
	do_action( 'bpdev_admin_save_component', 'bpdev-theme-buddybar-logo' );
	do_action( 'bpdev_theme_admin_menu_extra_save' );

}

function bpdev_theme_admin_menu_screen() {

	?>
	<h3><?php echo BPDEV_THEME_NAME; ?></h3>
	<table class="form-table">
		<tr valign="top">
			<th scope="row">Plugin Version</th>
			<td>
				<?php echo BPDEV_THEME_VERSION; ?>
			</td>
		</tr>
		<tr>
			<th scope="row">Plugin Status</th>
			<td>
				<select id="bpdev-theme-status" name="bpdev-theme-status">
					<option value="off"  <?php if ( !get_option( 'bpdev-theme-status' ) && get_option( 'bpdev-theme-status' ) == "off" ) echo 'selected=""'; ?>>Off</option>
					<option value="on" <?php if ( get_option('bpdev-theme-status') == "on") echo 'selected=""'; ?>>On</option>
				</select>
			</td>
		</tr>
		<tr valign="top">

			<th scope="row">BuddyBar Logo</th>
			<td>
				<input type="text" value="<?php echo get_option( 'bpdev-theme-buddybar-logo' ); ?>" id="bpdev-theme-buddybar-logo" name="bpdev-theme-buddybar-logo"/><br/>
					The complete url of the image es: <code>http://nicolagreco.com/logo.gif</code>
			</td>

		</tr>
<?php do_action('bpdev_theme_admin_menu_extra_screen'); ?>
	</table>
	<?php
}
?>